﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeBuilder.Entity
{
    public class CreateEntityBase
    {
        private string tableName;
        private string fileDir;
        private DataTable dataTable;
        private DataTable indexTable;
        public CreateEntityBase(string tableName, string fileDir, DataTable dataTable, DataTable indexTable)
        {
            this.tableName = tableName;
            this.fileDir = fileDir;
            this.dataTable = dataTable;
            this.indexTable = indexTable;
        }

        public void Run()
        {
            var indexRows = new List<DataRow>();
            foreach (DataRow row in this.indexTable.Rows)
            {
                indexRows.Add(row);
            }
            var entityName = "EntityBase";
            IList<string> contents = new List<string>();
            contents.Add($"using SQLite;");
            contents.Add($"");
            contents.Add($"namespace Hualong.TestWorldPro.Entities");
            contents.Add("{");
            contents.Add($"    public class EntityBase");
            contents.Add("    {");
            foreach (DataRow fieldItem in dataTable.Rows)
            {
                if (fieldItem[0].ToString() == "id" || fieldItem[0].ToString() == "sort"
                    || fieldItem[0].ToString() == "is_deleted" || fieldItem[0].ToString() == "create_user"
                    || fieldItem[0].ToString() == "create_time" || fieldItem[0].ToString() == "update_user"
                    || fieldItem[0].ToString() == "update_time")
                {
                    if (fieldItem[5].ToString() == "Extra")
                    {
                        contents.Add($"        [AutoIncrement]");
                    }
                    if (fieldItem[4].ToString() == "PRI")
                    {
                        contents.Add($"        [PrimaryKey]");
                    }
                    else
                    {
                        var indexRow = indexRows.Find(xx => xx[4].ToString() == fieldItem[1].ToString());
                        if (indexRow != null)
                        {
                            contents.Add($"        [Indexed(\"{indexRow[2].ToString()}\",  {indexRow[3]})]");
                        }
                    }
                    var notNull = "?";
                    if (fieldItem[2].ToString() == "NO")
                    {
                        contents.Add($"        [NotNull]");
                        notNull = "";
                    }
                    contents.Add($"        [Column(\"{fieldItem[0]}\")]");
                    contents.Add($"        public {DataTypeMap.Get(fieldItem[1].ToString())}{notNull} {Helper.GetPascalCase(fieldItem[0].ToString())} {{ get; set; }}");
                }
            }
            contents.Add("    }");
            contents.Add("}");
            string filePath = Path.Combine(fileDir, entityName);

            if (!Directory.Exists(filePath))
            {
                Directory.CreateDirectory(filePath);
            }
            filePath = Path.Combine(filePath, $"{entityName}.cs");
            if (File.Exists(filePath))
            { File.Delete(filePath); }
            File.WriteAllLines(filePath, contents, Encoding.UTF8);


            Console.WriteLine($"创建:{filePath} 完成！");
        }
    }
}
